package com.qcxk.yuqian.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.qcxk.yuqian.system.component.behaviour.BehaviourOperator;
import com.qcxk.yuqian.system.component.constant.SystemConstant;
import com.qcxk.yuqian.system.entity.Auth;
import com.qcxk.yuqian.system.service.AuthService;
import com.qcxk.yuqian.system.utils.RedisUtil;
import com.qcxk.yuqian.system.utils.ThmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private AuthService authService;
    @Autowired
    private RedisUtil redisUtil;

    @BehaviourOperator(desc = "查询权限信息")
    @RequestMapping(value = "/loadAuth")
    public JSONObject loadAuth(Auth auth) {
        JSONObject json = new JSONObject();
        auth = authService.getById(auth.getId());
        if (auth != null) {
            json.put("code", "0");
            json.put("data", auth);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "用户授权查询权限")
    @RequestMapping(value = "/queryAuthList")
    public JSONObject queryAuthList() {
        JSONObject json = new JSONObject();
        Integer id = Integer.parseInt((String) redisUtil.get(request.getHeader("token")));
        List<JSONObject> authJSON = authService.queryAllList(id);
        json.put("code", "0");
        json.put("data", authJSON);
        return json;
    }

    @BehaviourOperator(desc = "根据角色查询权限")
    @RequestMapping(value = "/queryRoleAuthList")
    public JSONObject queryRoleAuthList(Integer id) {
        JSONObject json = new JSONObject();
        List<JSONObject> authJSON = authService.queryRoleAuthList(id);
        json.put("code", "0");
        json.put("data", authJSON);
        return json;
    }

    @BehaviourOperator(desc = "查询所有菜单权限")
    @RequestMapping(value = "/queryAllList")
    public JSONObject queryAllList() {
        JSONObject json = new JSONObject();
        Integer id = Integer.parseInt((String) redisUtil.get(request.getHeader("token")));
        List<JSONObject> authJSON = authService.queryAllList();
        json.put("code", "0");
        json.put("data", authJSON);
        return json;
    }

    @BehaviourOperator(desc = "查询用户菜单权限")
    @RequestMapping(value = "/queryUserAuthList")
    public JSONObject queryUserAuthList() {
        JSONObject json = new JSONObject();
        Integer id = Integer.parseInt((String) redisUtil.get(request.getHeader("token")));
        List<JSONObject> authJSON = authService.queryAllList(id);
        json.put("data", authJSON);
        json.put("code", "0");
        return json;
    }

    @BehaviourOperator(desc = "新增权限")
    @RequestMapping(value = "/addAuth")
    public JSONObject addAuth(Auth auth) {
        JSONObject json = new JSONObject();
        auth.setCtime(ThmsUtil.getDate());

        if (authService.addAuth(auth)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "删除菜单")
    @RequestMapping(value = "/delAuth")
    public JSONObject delAuth(Auth auth) {
        JSONObject json = new JSONObject();
        //删除子集
        if (authService.removeById(auth.getId())) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "修改菜单")
    @RequestMapping(value = "/updateAuth")
    public JSONObject updateAuth(Auth auth) {
        JSONObject json = new JSONObject();

        if (authService.updateAuth(auth)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }
}
